﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RSHB.Framework.Data;
using RSHB.Frontend.UI.Controllers;
using RSHB.Frontend.UI.ViewModels;

namespace RSHB.Frontend.Controllers
{
    public class IndividualController : BaseRSHBController
    {
        BHDDataContext dataContext = new BHDDataContext();
        //
        // GET: /Individual/

        public ActionResult Index()
        {
            return View(new UserBrowseViewModel(dataContext.Users.ToList()));
        }
        
        [HttpGet]
        public ActionResult Create()
        {
            return View(new UserCreateViewModel());
        }

        [HttpPost]
        public ActionResult Create(UserCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                RSHB.Framework.Data.User user = new Framework.Data.User() { 
                    UserGuid = Guid.NewGuid(),
                    UserRowCreatedDate = DateTime.Now,
                    UserRowUpdatedDate = DateTime.Now
                };
                dataContext.Users.InsertOnSubmit(user);
                dataContext.SubmitChanges();

                VCard vcard = new VCard() { 
                    vcardNameLast = model.UserLastName,
                    vcardNameFirst = model.UserFirstName,
                    vcardNameMiddle = model.UserMiddleName,
                    vcardBirthday = model.Birthday,
                    vcardPassportSeries = model.PassportSeries,
                    vcardPassportNum = model.PassportNum,
                    vcardPassportIssueDate = model.PassportIssueDate,
                    vcardPassportIssuerName = model.PassportIssuerName,
                    vcardPassportIssuerCode = model.PassportIssuerCode,
                    vcardAddressHomeArea = model.ActualAddress,
                    vcardAddressBusinessArea = model.RegisterAddress,
                    UserID = user.UserID
                };

                dataContext.VCards.InsertOnSubmit(vcard);
                dataContext.SubmitChanges();

                this.ShowInfoMessage(string.Format("Физ.лицо {0} {1} {2} создано", model.UserLastName, model.UserFirstName, model.UserMiddleName));
                return RedirectToAction("Index");

            }
            else
            {
                return View(model);
            }
        }

        public ActionResult Edit(int id)
        {
            RSHB.Framework.Data.User user = dataContext.Users.Where(u => u.UserID == id).SingleOrDefault();
            VCard vCard = user.VCards.SingleOrDefault();
            return View(new UserEditPropertiesViewModel() { 
                UserID = user.UserID,
                UserFirstName = vCard.vcardNameFirst,
                UserMiddleName = vCard.vcardNameMiddle,
                UserLastName = vCard.vcardNameLast,
                Birthday = vCard.vcardBirthday.Value,
                PassportSeries = vCard.vcardPassportSeries,
                PassportNum = vCard.vcardPassportNum,
                PassportIssueDate = vCard.vcardPassportIssueDate.Value,
                PassportIssuerName = vCard.vcardPassportIssuerName,
                PassportIssuerCode = vCard.vcardPassportIssuerCode,
                ActualAddress = vCard.vcardAddressHomeArea,
                RegisterAddress = vCard.vcardAddressBusinessArea
            });
        }

        [HttpPost]
        public ActionResult Edit(UserEditPropertiesViewModel model)
        {
            if (ModelState.IsValid)
            {
                RSHB.Framework.Data.User user = dataContext.Users.Where(u => u.UserID == model.UserID).SingleOrDefault();
                
                VCard vCard = user.VCards.SingleOrDefault();
                vCard.vcardNameLast = model.UserLastName;
                vCard.vcardNameFirst = model.UserFirstName;
                vCard.vcardNameMiddle = model.UserMiddleName;
                vCard.vcardBirthday = model.Birthday;
                vCard.vcardPassportSeries = model.PassportSeries;
                vCard.vcardPassportNum = model.PassportNum;
                vCard.vcardPassportIssueDate = model.PassportIssueDate;
                vCard.vcardPassportIssuerName = model.PassportIssuerName;
                vCard.vcardPassportIssuerCode = model.PassportIssuerCode;
                vCard.vcardAddressHomeArea = model.ActualAddress;
                vCard.vcardAddressBusinessArea = model.RegisterAddress;

                dataContext.SubmitChanges();
                this.ShowInfoMessage(string.Format("Физ.лицо {0} {1} {2} изменено", model.UserLastName, model.UserFirstName, model.UserMiddleName));                
                return RedirectToAction("Index");
            }
            else
            {
                return View(model);
            }
        }
    }
}

